package com.vcaresolution.logistic.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vcaresolution.logistic.form.CountryForm;
import com.vcaresolution.logistic.pojo.Country;
import com.vcaresolution.logistic.to.CountryTO;

@Service
public class CountryServiceImpl extends BaseService implements CountryService{

	private Country populateCountryPojo(CountryForm sectorForm){
		Country sector = new Country();
		BeanUtils.copyProperties(sectorForm, sector);
		
		return sector;
	}
	
	@Override
	@Transactional
	public void add(CountryForm sectorForm) {
		Country sector = populateCountryPojo(sectorForm);
		
		getDaoFactory().getUtilityDAO().add(sector);
	}

	@Override
	@Transactional
	public void update(CountryForm sectorForm) {
		Country sector = populateCountryPojo(sectorForm);
		
		getDaoFactory().getUtilityDAO().update(sector);
	}

	@Override
	@Transactional
	public void delete(CountryForm sectorForm) {
		getDaoFactory().getUtilityDAO().delete(populateCountryPojo(sectorForm));
	}

	@Override
	@Transactional
	public CountryTO findById(Serializable sectorID) {
		Country sector = (Country) getDaoFactory().getUtilityDAO().findById(Country.class,sectorID);
		
		CountryTO sectorTO = new CountryTO();
		BeanUtils.copyProperties(sector, sectorTO);
		
		return sectorTO; 
	}

	@Override
	@Transactional
	public List<CountryTO> findByAll() {
		List<Country> sectorList = getDaoFactory().getUtilityDAO().findByAll(Country.class);
		
		List<CountryTO> sectorFormList = new ArrayList<CountryTO>();
		
		if( sectorList != null && !sectorList.isEmpty() ){
			CountryTO sectorForm = null;
			for(Country sector: sectorList){
				sectorForm = new CountryTO();
				
				BeanUtils.copyProperties(sector, sectorForm);
				sectorFormList.add(sectorForm);
			}
		}
		
		return sectorFormList;
	}

}
